﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace Kodi.Picturethrill
{
    public class Settings
    {
        public const string CompanyName = "Aristov";
        public const string AppName = "Picturethrill";
        public const string UpdateName = "PicturethrillUpdate";
        private const string SettingsName = "Settings.xml";

        public readonly static Version Windows7Version = new Version("6.1");

        #region Private Fields

        // use "My Pictures" as default folder name
        private string imageDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), Settings.AppName);
        private List<string> selectedUrls = new List<string>();
        private bool loaded;
        private string SettingsPath = Path.Combine(picturethrillAppPath, SettingsName);

        private bool updateDesktopBackground = true; // default
        private bool saveNewPictures = false; // default
        private bool automaticUpdate = true; // default

        private static string picturethrillAppPath = Path.Combine(Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%"), CompanyName, AppName);
        private static string picturethrillTempPath = Path.Combine(Path.GetTempPath(), CompanyName, AppName);
        private static string wallpaperPreviewsPath = Path.Combine(picturethrillTempPath, "WallpaperPreviews");
        private static string wallpaperDownloadsPath = Path.Combine(picturethrillTempPath, "WallpaperDownloads");
        private static string wallpaperBmpsPath = Path.Combine(picturethrillTempPath, "WallpaperBmps");
        private static string wallpaperCachePath = Path.Combine(picturethrillTempPath, "WallpaperCache");
        #endregion

        #region Public Properties


        public string ImageDirectory
        {
            get
            {
                return this.imageDirectory;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.imageDirectory = string.Empty;
                }
                else
                {
                    this.imageDirectory = value;
                }
            }
        }

        public bool Loaded
        {
            get
            {
                return this.loaded;
            }
        }

        public IList<string> SelectedUrls
        {
            get
            {
                return this.selectedUrls;
            }
        }

        public bool UpdateDesktopBackground
        {
            get
            {
                return this.updateDesktopBackground;
            }
            set
            {
                this.updateDesktopBackground = value;
            }
        }

        public bool SaveNewPictures
        {
            get
            {
                return this.saveNewPictures;
            }
            set
            {
                this.saveNewPictures = value;
            }
        }

        public bool AutomaticUpdate
        {
            get
            {
                return this.automaticUpdate;
            }
            set
            {
                this.automaticUpdate = value;
            }
        }

        /// <summary>
        /// Returns temporary path for this project. 
        /// If path doesn't exist it will be created on first call.
        /// </summary>
        public static string TempPath
        {
            get
            {
                if (!Directory.Exists(picturethrillTempPath))
                {
                    Directory.CreateDirectory(picturethrillTempPath);
                }

                return picturethrillTempPath;
            }
        }

        private static string CreateIfNotExists(string directoryName)
        {
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            return directoryName;
        }

        public static string WallpaperPreviewsPath
        {
            get
            {
                return CreateIfNotExists(wallpaperPreviewsPath);
            }
        }

        public static string WallpaperDownloadsPath
        {
            get
            {
                return CreateIfNotExists(wallpaperDownloadsPath);
            }
        }

        public static string WallpaperBmpsPath
        {
            get
            {
                return CreateIfNotExists(wallpaperBmpsPath);
            }
        }

        public static string CurrentProcessDirectoryPath
        {
            get
            {
                return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            }
        }

        public static string WallpaperCachePath
        {
            get
            {
                return CreateIfNotExists(wallpaperCachePath);
            }
        }

        public static string AppPath
        {
            get
            {
                if (!Directory.Exists(picturethrillAppPath))
                {
                    Directory.CreateDirectory(picturethrillAppPath);
                }

                return picturethrillAppPath;
            }
        }

        #endregion

        #region Public Methods

        public void Load()
        {
            // TODO: Later we should care about version, but not for now.
            if (!File.Exists(this.SettingsPath))
            {
                return;
            }

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(this.SettingsPath);

            XmlNode settingsNode = xmlDocument.SelectSingleNode("Settings");

            this.ImageDirectory = XmlUtilities.GetValue(settingsNode, "Directory", this.ImageDirectory);
            this.UpdateDesktopBackground = XmlUtilities.GetValue(settingsNode, "UpdateDesktopBackground", true);
            this.SaveNewPictures = XmlUtilities.GetValue(settingsNode, "SaveNewPictures", false);
            this.AutomaticUpdate = XmlUtilities.GetValue(settingsNode, "AutomaticUpdate", true);

            this.selectedUrls.Clear();
            foreach (XmlElement selectedUrlNode in xmlDocument.SelectNodes("Settings/SelectedUrls/SelectedUrl"))
            {
                this.selectedUrls.Add(selectedUrlNode.InnerText);
            }

            this.loaded = true;
        }

        public void Save()
        {
            using (Log.Group())
            {
                string settingsDirectory = Path.GetDirectoryName(SettingsPath);
                if (!Directory.Exists(settingsDirectory))
                {
                    Directory.CreateDirectory(settingsDirectory);
                }

                XmlDocument xmlDocument = new XmlDocument();
                XmlElement settingsNode = xmlDocument.CreateElement("Settings");

                xmlDocument.AppendChild(settingsNode);

                XmlUtilities.AddChild(settingsNode, "Directory", this.ImageDirectory);
                XmlUtilities.AddChild(settingsNode, "UpdateDesktopBackground", this.UpdateDesktopBackground);
                XmlUtilities.AddChild(settingsNode, "SaveNewPictures", this.SaveNewPictures);
                XmlUtilities.AddChild(settingsNode, "AutomaticUpdate", this.AutomaticUpdate);

                XmlElement selectedUrlsNode = xmlDocument.CreateElement("SelectedUrls");
                settingsNode.AppendChild(selectedUrlsNode);

                foreach (string selectedUrl in this.selectedUrls)
                {
                    XmlUtilities.AddChild(selectedUrlsNode, "SelectedUrl", selectedUrl);
                }

                xmlDocument.Save(this.SettingsPath);
                Log.Comment("Settings saved to: " + this.SettingsPath);
            }
        }

        #endregion
    }
}
